package eai.tp1.core.datamodel;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.jdom.Attribute;
import org.jdom.DocType;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

/**
 * This class store the list of digital cameras as a xml file. 
 * We've used the web xsd validator: http://tools.decisionsoft.com/schemaValidate/results.jsp
 * @author Joel
 *
 */
public class DataManager {

    private Document doc;

    /**
     * This method writes the camera list into a xml document, following the CameraSearch.xsd
     * 
     * @param cameraList
     * @param brand
     * @param web_source
     */
    public void write(List<DigitalCamera> cameraList, String brand, String web_source) {
        
    	Element root = new Element("digital_camera_list");
    	Namespace xsi = Namespace.getNamespace("xsi","http://www.w3.org/2001/XMLSchema-instance");     	
    	root.setAttribute("noNamespaceSchemaLocation","CameraSearch.xsd",xsi);     	         
        root.setAttribute("brand", brand);
        root.setAttribute("web_source", web_source);
        doc = new Document();

        for (DigitalCamera camera : cameraList) {
            Element cameraElement = new Element("digital_camera");
            cameraElement.setAttribute(DigitalCamera.ID, "" + camera.getId());

            addAtribute(cameraElement, camera.getName(), DigitalCamera.NAME);
            addAtribute(cameraElement, camera.getDescription(), DigitalCamera.DESCRIPTION);
            addAtribute(cameraElement, camera.getDate().getYear()+"-"+camera.getDate().getMonth()+"-"+camera.getDate().getDay(), DigitalCamera.DATE);
            addAtribute(cameraElement, camera.getMaxRes(), DigitalCamera.MAX_RES);
            addAtribute(cameraElement, camera.getMinRes(), DigitalCamera.MIN_RES);
            addAtribute(cameraElement, camera.getImageRatio(), DigitalCamera.IMAGE_RATIO);
            addAtribute(cameraElement, camera.getEffectivePixels(), DigitalCamera.EFFECTIVE_PIXELS);
            addAtribute(cameraElement, camera.getSensorSize(), DigitalCamera.SENSOR_SIZE);
            addAtribute(cameraElement, camera.getIsoRating(), DigitalCamera.ISO_RATING);
            addAtribute(cameraElement, camera.getMaxShutter(), DigitalCamera.MAX_SHUTTER);
            addAtribute(cameraElement, camera.getMinShutter(), DigitalCamera.MIN_SHUTTER);
            addAtribute(cameraElement, camera.getReviewLink(), DigitalCamera.REVIEW_LINK);
            addAtribute(cameraElement, camera.getImageLink(), DigitalCamera.PICTURE_LINK);

            root.addContent(cameraElement);
        }        
        doc.setRootElement(root);
    }

    private void addAtribute(Element el, String attribute, String tag) {

        Element temp = new Element(tag);
        temp.addContent(attribute);
        el.addContent(temp);
    }

    public void print(List<DigitalCamera> cameras) {
        for (DigitalCamera cam : cameras) {
            System.out.println("nome: " + cam.getName());
            System.out.println("min shutter: " + cam.getMinShutter());
        }
    }

    public Document getDocument() {
    	return this.doc;
    }
    /**
     * This method saves the document into disk
     * @param fileName
     */
    public void createXML(String fileName) {

        try {
            XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
            FileWriter writer = new FileWriter(System.getProperty("user.dir")+File.separatorChar+"data"+File.separator+fileName);
            outputter.output(doc, writer);
            writer.close();
            System.out.println(fileName+" XML: Successfuly Done.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
